package org.raycloud.service;


import com.gargoylesoftware.htmlunit.AjaxController;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Administrator on 2014/4/4.
 * Time: 16:57
 */
public class Htmlunitdemo {
    public static void main(String[] args) {

        getCookies();
    }

    public static String getCookies() {
        final WebClient webClient=new WebClient(BrowserVersion.FIREFOX_3_6){
//            /**
//             * 重写 异常抛出类，对加载失败的 js 做忽略处理
//             * @param webResponse
//             */
//            @Override
//            public void throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse webResponse) {
//
//                final int statusCode = webResponse.getStatusCode();
//                final boolean successful = (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES)
//                        || statusCode == HttpStatus.SC_USE_PROXY
//                        || statusCode == HttpStatus.SC_NOT_MODIFIED;
//
//                if (isThrowExceptionOnFailingStatusCode() && !successful && !webResponse.getWebRequest().getUrl().toString().equals("http://www.hunnu.edu.cn/_sitegray.js") ) {
//                    throw new FailingHttpStatusCodeException(webResponse);
//                }
//            }
        };

        Long l1 = System.currentTimeMillis();
        StringBuilder sb=new StringBuilder();
        webClient.setCssEnabled(true);
        webClient.setJavaScriptEnabled(true);
        webClient.setAjaxController(new AjaxController());

//        webClient.setIncorrectnessListener(new IncorrectnessListener() {
//            @Override
//            public void notify(String message, Object origin) {
//                System.out.println("异常？js？");
//            }
//        });


//        webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
//            @Override
//            public void scriptException(HtmlPage htmlPage, ScriptException scriptException) {
//                System.out.println("scriptException");
//            }
//
//            @Override
//            public void timeoutError(HtmlPage htmlPage, long allowedTime, long executionTime) {
//                System.out.println("timeoutError");
//            }
//
//            @Override
//            public void malformedScriptURL(HtmlPage htmlPage, String url, MalformedURLException malformedURLException) {
//                System.out.println("malformedScriptURL");
//            }
//
//            @Override
//            public void loadScriptError(HtmlPage htmlPage, URL scriptUrl, Exception exception) {
//                System.out.println("loadScriptError");
//            }
//        });

//        webClient.setIncorrectnessListener();

//        webClient.setThrowExceptionOnScriptError(false);


        webClient.setTimeout(50000);
//        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
//        webClient.setThrowExceptionOnScriptError(false);
        HtmlPage page=null;

        try {
//            page = webClient.getPage("http://www.oschina.net/");
            page= webClient.getPage("http://rate.taobao.com/user-rate-1763577710.htm");
//            page=webClient.getPage("http://www.17k.com/chapter/753884/15684367.html");
//            webClient.getPage()



           Set set=webClient.getCookieManager().getCookies();
           Iterator it= set.iterator();
            while (it.hasNext())
            {
                com.gargoylesoftware.htmlunit.util.Cookie cookie= (com.gargoylesoftware.htmlunit.util.Cookie) it.next();
                sb.append(cookie.getName());
                sb.append("=");
                sb.append(cookie.getValue());
                sb.append(";");
            }
            System.out.println(sb.toString());



//            page=webClient.getPage("http://www.baid11u.com/");
        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(page.asXml());

   /*     HtmlForm form = page.getForms().get(0);
        System.out.println(form.asXml());

        HtmlElement textUsername = form.getInputByName("user_name");
        HtmlElement textPwd = form.getInputByName("user_passwd");
        System.out.println(textUsername.asXml());
        System.out.println(textPwd.asXml());
        try {
            textUsername.click();
            textUsername.type("左韵");
            textPwd.click();
            textPwd.type("jik920717");
        } catch (IOException e) {
            e.printStackTrace();
        }
        final HtmlSubmitInput button = form.getInputByName("btnLogin");
        try {
           HtmlPage page2= button.click();
            System.out.println(page2.asText());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Set<Cookie> set= webClient.getCookieManager().getCookies();
        Iterator it=set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }*/
        webClient.closeAllWindows();
        Long l2 = System.currentTimeMillis()-l1;
        System.out.println("处理时间"+l2+"ms");

        return sb.toString();
    }
}

